﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;

public partial class account_Favoriteproduct : System.Web.UI.Page
{
    private List<Product> favoriteProducts;
    protected void Page_Load(object sender, EventArgs e)
    {
        getFavoriteProduct();
    }
    public class Product
    {

        public int Id { get; set; }
        //需要
        public string Name { get; set; }
        //需要
        public decimal Price { get; set; }
        //需要
        public byte Discount { get; set; }
        //需要
        public string Description { get; set; }

        public string Shop { get; set; }

        public short InventoryLevel { get; set; }
        //需要
        public int? CoverPhoto { get; set; }

        public string Category { get; set; }


    }

    private void getFavoriteProduct() {
        var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"];
        if (connString == null)
            throw new ArgumentNullException("无法获取指定的数据库连接字符串。");
        string userId = Membership.GetUser().ProviderUserKey.ToString();

        using (SqlConnection connection = new SqlConnection(connString.ConnectionString))
        {
            connection.Open();

            SqlCommand command = new SqlCommand(
                string.Format(@"
SELECT   Product.name, Product.category
FROM      FavoriteProduct INNER JOIN
                Product ON FavoriteProduct.product = Product.id
WHERE   (FavoriteProduct.[user] = '{0}')", userId),
                connection);
            command.CommandType = System.Data.CommandType.Text;
            var reader = command.ExecuteReader();

            favoriteProducts = new List<Product>();
            while (reader.Read())
            {
                favoriteProducts.Add(new Product
                {
                    Name = reader.GetString(0),
                    Category = reader.GetString(1),
                });
            }
        }
    
    }
    protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.Result = favoriteProducts;
    }
}